package org.jd.export.service;

import lombok.RequiredArgsConstructor;
import org.jd.export.controller.SchemaController;
import org.jd.export.model.ColumnInfo;
import org.jd.export.model.TableInfo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class TestService implements CommandLineRunner {

    private final SchemaService schemaService;
    private final SchemaController schemaController;

    @Override
    public void run(String... args) throws Exception {
        // 获取并打印数据库中所有表的信息
        String databaseName = schemaController.extractDatabaseName();
        System.out.println("正在读取数据库: " + databaseName + " 的表结构信息...\n");
        
        List<TableInfo> tables = schemaService.getAllTablesInSchema(databaseName);
        
        for (TableInfo table : tables) {
            System.out.println("表名: " + table.getTableName());
            System.out.println("表描述: " + (table.getTableComment() != null ? table.getTableComment() : "无描述"));
            System.out.println("字段信息:");
            
            for (ColumnInfo columnInfo : table.getColumnInfos()) {
                System.out.printf(
                    "  %s - %s (允许为空: %s) - %s%n",
                    columnInfo.getColumnName(),
                    columnInfo.getColumnType(),
                    "YES".equals(columnInfo.getIsNullable()) ? "是" : "否",
                    columnInfo.getColumnComment() != null ? columnInfo.getColumnComment() : "无说明"
                );
            }
            System.out.println("----------------------------------------");
        }
        // 方式1: 使用默认路径（从配置文件读取）
        schemaService.exportDbToWord(tables);
        
        // 方式2: 指定自定义路径
        // String outputPath = "src/main/resources/database_schema.docx";
        // schemaService.exportDbToWord(tables, outputPath);
        
        // 方式3: 其他路径选项
        // String outputPath = "target/database_schema.docx";  // Maven构建目录
        // String outputPath = System.getProperty("user.home") + "/Desktop/database_schema.docx";  // 桌面
        // String outputPath = "./database_schema.docx";  // 项目根目录
    }
}
